<?php
	require_once("../configuration.php");
	require_once("../includes/db/constants.php");
	$useronlinesql = sprintf("SELECT * FROM ".TBL_ADMIN_USER_ONLINE." WHERE ip_address = '{$_SERVER['REMOTE_ADDR']}' AND user_agent = '{$_SERVER['HTTP_USER_AGENT']}'");
	$data = $db->get_row($useronlinesql);
	if (!isset($_SESSION['user_id'])) {
		if($data!=""){
			 $res1=(sprintf("UPDATE ".TBL_ADMIN_USER_ONLINE." SET activity=now(),member='n' WHERE session_id='" .session_id(). "'"));
			$db->query($res1);
		}
		else{
	   		$res2=(sprintf("INSERT INTO ".TBL_ADMIN_USER_ONLINE." (session_id,activity,ip_address,refurl,user_agent) VALUES ('" .session_id(). "', now(), '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['HTTP_USER_AGENT']}')"));
			$db->query($res2);
		}
	} else {
		if($data!=""){
			 $db->query(sprintf("UPDATE ".TBL_ADMIN_USER_ONLINE." SET activity=now(),member='y' WHERE session_id='" .session_id(). "'"));
		}else{
			$res2=(sprintf("INSERT INTO ".TBL_ADMIN_USER_ONLINE." (session_id,activity,ip_address,refurl,user_agent) VALUES ('" .session_id(). "', now(), '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['HTTP_USER_AGENT']}')"));
			$db->query($res2);
		}	
	       
	} 
	
	if (isset($_SESSION['user_id'])) {  
		$res3=sprintf("UPDATE ".TBL_ADMIN_USER_ONLINE." SET activity=now(),refurl ='".$_SERVER['HTTP_REFERER']."'  WHERE session_id='" .session_id(). "'");
		$db->query($res3);
	}  
	if (date('H') == 00) { 
		$db->query(sprintf("TRUNCATE TABLE ".TBL_ADMIN_USER_ONLINE)); 
	}
	$db->query(sprintf("DELETE FROM ".TBL_ADMIN_USER_ONLINE." WHERE ADDTIME(activity,'00:15:00')<now()"));
?>